package com.sudo.boot.service.sys.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.mybatisflex.core.util.StringUtil;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.sudo.boot.base.util.IfFail;
import com.sudo.boot.mapper.sys.entity.SysConfig;
import com.sudo.boot.mapper.sys.mapper.SysConfigMapper;
import com.sudo.boot.service.sys.SysConfigService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static com.sudo.boot.mapper.sys.entity.table.SysConfigTableDef.SYS_CONFIG;

/**
 * 系统菜单  服务层实现。
 *
 * @author admins
 * @since 2023-10-13
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Override
    public void updateConfigByKey(Class<?> key, Object object) {
        SysConfig sysConfig = new SysConfig();
        sysConfig.addConfig(object);

        String toUnderline = StringUtil.camelToUnderline(key.getSimpleName());
        IfFail.dealInt(() -> mapper.updateByCondition(sysConfig, SYS_CONFIG.KEY.eq(toUnderline)));
    }

    @Override
    public Map<String,Object> getSysConfigMapByKey(Class<?> key) {
        String toUnderline = StringUtil.camelToUnderline(key.getSimpleName());
        SysConfig sysConfig = mapper.selectOneByCondition(SYS_CONFIG.KEY.eq(toUnderline));
        Map<String,Object> configMap = JSON.parseObject(JSON.toJSONString(sysConfig), new TypeReference<HashMap<String, Object>>(){});
        configMap.putAll(sysConfig.getConfig());
        return configMap;
    }
}
